package com.bhxx.fund.task;

import com.alibaba.fastjson.JSONObject;
import com.bhxx.fund.entry.FundDTO;
import com.bhxx.fund.service.MailService;
import com.bhxx.fund.mapper.TmApiMapper;
import com.bhxx.fund.mapper.TmFundMapper;
import com.bhxx.fund.mapper.TmUserMailMapper;
import com.bhxx.fund.pojo.TmFund;
import com.bhxx.fund.pojo.TmUserMail;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.List;

@Component
public class MyTask {


    @Autowired
    private MailService mailService;


    @Autowired
    RestTemplate restTemplate;

    @Autowired
    TmApiMapper apiMapper;
    @Autowired
    TmFundMapper fundMapper;
    @Autowired
    TmUserMailMapper userMailMapper;


    @Scheduled(cron = "0 45,55 14 ? * MON-FRI")
//    @Scheduled(cron = "0 0 7 * * ?")
    public void fundTimingTask() {
        int sendNumber=0;
        List<TmUserMail> tmUserMails = userMailMapper.selectAllUserMail();
        StringBuffer mailContent = new StringBuffer();
        for (TmUserMail tmUserMail : tmUserMails) {
            String mailId = tmUserMail.getMailId();
            List<TmFund> tmFunds = fundMapper.selectById(mailId);
            for (TmFund tmFund : tmFunds) {
                FundDTO fundDTO = getCurrentFund(tmFund);
                String gszzl = fundDTO.getGszzl();
                sendNumber = ruleToCompute(gszzl, tmFund.getInitCondition());
                mailContent = getToMailbody(fundDTO, mailContent);
            }
            mailService.sendMail(tmUserMail.getMail(), "基金变动", mailContent.toString(),sendNumber);
        }

    }


    public StringBuffer getToMailbody(FundDTO fundDTO, StringBuffer mailContent) {

        if (StringUtils.isBlank(fundDTO.getFildMsg())) {
            mailContent.append(fundDTO.toString());
        } else {
            mailContent.append(fundDTO.getFildMsg());
        }
        return mailContent;
    }

    public FundDTO getCurrentFund(TmFund tmFund) {
        FundDTO fundDTO = null;

        String apiURL = "http://fundgz.1234567.com.cn/js/" + tmFund.getFundId() + ".js?rt=1463558676006";
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(apiURL, String.class);
        if (200 == responseEntity.getStatusCodeValue()) {
            //jsonpgz({"fundcode":"161725","name":"招商中证白酒指数(LOF)A",
            // "jzrq":"2021-10-14","dwjz":"1.4011","gsz":"1.3924","gszzl":"-0.62","gztime":"2021-10-15 15:00"});
            String body = responseEntity.getBody();
            String j1 = StringUtils.remove(body, "jsonpgz(");
            String remove = StringUtils.remove(j1, ");");
            fundDTO = JSONObject.parseObject(remove, FundDTO.class);
        } else {

            String content = "此基金：" + tmFund.getFundName() + "/n" +
                    "基金编号：" + tmFund.getFundId() + "查询失败" + responseEntity.getStatusCode();
            fundDTO.setFildMsg(content);
        }
        return fundDTO;
    }

    public int ruleToCompute(String gszzl, String initCondition) {
        BigDecimal init = BigDecimal.valueOf(1);
        if (StringUtils.isNotBlank(gszzl)) {
            BigDecimal current = new BigDecimal(gszzl).abs();
            if (StringUtils.isNoneBlank(initCondition)){
                init = new BigDecimal(initCondition).abs();
            }
            BigDecimal subtract = init.subtract(current).abs();

            if (subtract.compareTo(BigDecimal.ONE)>0){
                return subtract.intValue();
            }
        }
        return 0;
    }
}
